#!/bin/bash
# 完全清理所有服务和数据

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
DOCKER_DIR="$PROJECT_ROOT/docker"

echo "🧹 清理所有服务和数据..."
echo "=================================================="
echo "⚠️  警告: 这将删除所有容器、卷和数据！"
echo ""
read -p "确定要继续吗？(yes/no): " -r
echo ""

if [[ ! $REPLY =~ ^[Yy]es$ ]]; then
    echo "❌ 已取消"
    exit 0
fi

# 切换到项目根目录
cd "$PROJECT_ROOT"

# 停止并删除容器、网络、卷
echo "🗑️  删除容器和卷..."
docker-compose -f docker/docker-compose.yml --env-file .env down -v

# 清理 LocalStack 创建的 Lambda 执行容器
echo "🧹 清理 Lambda 执行容器..."
LAMBDA_CONTAINERS=$(docker ps -a --filter "name=browse-etl-localstack-lambda" --format "{{.ID}}" 2>/dev/null || true)
if [ -n "$LAMBDA_CONTAINERS" ]; then
    echo "$LAMBDA_CONTAINERS" | xargs docker rm -f 2>/dev/null || true
    echo "✅ 已清理 Lambda 容器"
fi

# 清理构建缓存
echo "🗑️  清理构建缓存..."
docker-compose -f docker/docker-compose.yml --env-file .env build --no-cache event-processor 2>/dev/null || true

echo ""
echo "✅ 清理完成！"
echo ""
echo "📝 下一步："
echo "   重新启动: ./scripts/start-all.sh"
echo "=================================================="

